<template>
  <div id="TakingProducts">
    <van-nav-bar title="服用产品"  
      left-arrow 
      left-text="返回"
      @click-left="onClickLeft"
      fixed 
      placeholder></van-nav-bar>
    <div class="product">
      <div class="header">
        <van-row type="flex" justify="space-between">
          <van-col span="6">产品名称</van-col>
          <van-col span="6">服用剂量</van-col>
        </van-row>
      </div>
      <div class="addFood">
        <van-form @submit="onSubmit" v-if="addFood">
          <div >
            <van-field
              v-model="message"
              rows="4"
              autosize
              label=""
              placeholder="请输入口服药物"
            />
          </div>
          <div class="btn_box" >
            <van-button type="default" @click="cancel">取消</van-button>
            <van-button type="info" native-type="submit">确认</van-button>
          </div>
        </van-form>
        <van-button class="addBtn"  style="width:60vw;height:40px" v-if="!addFood"  type="info" @click="show">新增产品</van-button>
      </div>
      <div class="content">
        <van-row type="flex" justify="space-between" v-for="(item,idx) in list" :key="idx">
          <van-checkbox v-model="check[idx]" @click="toggle(idx)" ref="checkboxes">{{ item.title }}</van-checkbox>
          <!-- <van-col span="6">{{ item.title }}</van-col> -->
          <van-col span="6">
            <van-field v-model="item.nums"  :rules="[{required: true, message: '请输入服用剂量' }]"/>
          </van-col>
        </van-row>
      </div>
      <van-button v-if="sureShow" type="info" @click="sure">确定</van-button>
    </div>
  </div>
</template>

<script>
import { chufangoption } from "@/apis/record";
import { optionAdd } from "@/apis/record";
export default {
  data() {
    return {
      addFood:false,
      sureShow:true,
      message:'',
      value: "",
      index: "",
      list: [],
      listOral: [],
      idx: "",
      check:[]
    };
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1); //返回上一层
    },
    chufang() {
      chufangoption({ ty: 10, pid: 0 })
        .then((res) => {
          this.list = res.data.list;
          for (let i = 0; i < this.list.length; i++) {
            this.check[i] = false
          }
          console.log(this.list)
        })
        .catch((err) => {
          if(err.msg){
            this.$toast(err.msg);
          }else{
            this.$toast("网络错误"); 
          }
        });
    },

    sure() {
      let obj = JSON.parse(localStorage.getItem("medicine"));
      let obj1 = JSON.parse(localStorage.getItem("form"));
      let list = this.list.filter((item) => {
        if (item.nums != "") {
          return item;
        }
      });
      for (let i = 0; i < this.check.length; i++) {
        let ch = this.check
        if(ch[i] == true){
         
         if( obj[this.index].info == ""){
           
           obj[this.index].info = this.list[i].title + ',' + this.list[i].nums
         }
         else{
           obj[this.index].info = obj[this.index].info + ';' +  this.list[i].title + ',' + this.list[i].nums
         }
        }
      }
      localStorage.setItem("medicine", JSON.stringify(obj));
      localStorage.setItem("form", JSON.stringify(obj1));
      this.$router.go(-1);
    },
    toggle(idx) {
      console.log(this.check)
    },
    show() {
      this.addFood = true,
      this.sureShow = false
    },
    cancel() {
      this.sureShow = true
      this.addFood = false
      this.message = ''

    },
    onSubmit() {
      optionAdd({pid:0,ty:10,title:this.message})
      .then((res) => {
          let add = {id:'',info:'',title:'',video:'',img:'',nums:'0'}
          add.id = res.data.id
          add.title = this.message
          this.list.push(add)
          console.log(this.list)
          this.cancel()
        })
        .catch((err) => {
          if(err.msg){
            this.$toast(err.msg);
          }else{
            this.$toast("网络错误"); 
          }
        });
    }
  },
  mounted() {
    this.chufang();
  },
  created() {
    this.index = this.$route.params.index;
    console.log(this.index);
  },
};
</script>

<style lang="less">
#TakingProducts {
  text-align: left;
  background-color: #fff;
  margin-bottom: 50px;
  .van-nav-bar {
    background-color: #ededed;
  }
  .product {
    padding: 26px 20px;
    .header {
      border-bottom: 1px solid #f5f5f5;
      padding-bottom: 19px;
      color: #999999;
    
    }
    .btn_box{
      margin: 0 auto;
      display:flex;
      justify-content: space-between;
      align-items: center;
      width:60vw;
      button{
        width: 80px;
        height: 40px;
      }
    }
    .addBtn{
      width:60vw;
      height:40px;
      margin:30px 15vw;
    }
    .content {
      .van-row {
        padding: 15px 0;
        border-bottom: 1px solid #f5f5f5;
        .van-cell {
          padding: 0;
          input {
            width: 70px;
            height: 26px;
            border-radius: 8px;
            text-align: center;
            border: 1px solid rgba(221, 221, 221, 1);
          }
        }
      }
    }
    .van-button {
      width: 100%;
      height: 50px;
      line-height: 50px;
      font-size: 16px;
      margin-top: 30px;
    }
  }
  .addFood{
    margin-top: 10px;
    .van-field{
      border:1px solid #999999;
    }
  }
}
</style>
